package com.inews.demo3;

import java.net.InetSocketAddress;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSessionConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class SmsServer {

	private static Logger logger = Logger.getLogger(SmsServer.class);

	private static final int PORT = 3005;

	public static void main(String[] args) {
		IoAcceptor acceptor = null;
		try {
			acceptor = new NioSocketAcceptor();
			// 设置过滤器
			acceptor.getFilterChain().addLast(
					"codec",
					new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
			// 获得IoSessionConfig对象
			IoSessionConfig cfg = acceptor.getSessionConfig();
			// 读写通道10秒内无操作进入空闲状态
			cfg.setIdleTime(IdleStatus.BOTH_IDLE, 100);

			// 绑定逻辑处理器(重要)
			acceptor.setHandler(new SmsServerHandler());

			// 绑定端口
			acceptor.bind(new InetSocketAddress(PORT));
			
			logger.info("服务端demo3启动成功...     端口号为：" + PORT);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
